﻿@using OPM.Class;
@using System.Web.Mvc;
@using OPM.MultiResource

@{
    Layout = "~/Views/Shared/_LayoutPopup.cshtml";
    var selectedCountryId = Request["country_id"];
    var selectedProvinceId = Util.ConvertToIntNull( Request["province_id"] );
    var selectedDistrictId = Util.ConvertToIntNull(Request["district_id"]);
    var selectedSubDistrictId = Util.ConvertToIntNull(Request["subdistrict_id"]);

}

	<div class="service_search_header">
		<div class="service_title">@Multi.case_area</div>
	</div>

	<div class="service_search_header2">
		<div class="service_title">@Multi.case_area_data</div>
	</div>
	<div class="service_box_table">
    <table id="service_table">
        <tr>
            <td>@Multi.country</td>
            <td>
                <select id="country_id" name="country_id">
                    @{
                        var countries = WSL.GetCountry();
                        string selectedCountryText = "";
                        string thaiVal = countries.Where(o => o.text == "ไทย" || o.text == "Thailand").Select(o => o.val).FirstOrDefault();
                        bool selectThai = string.IsNullOrWhiteSpace(selectedCountryId) || thaiVal == selectedCountryId;
                    }
                    @foreach (Country ccc in countries)
                    {
                        <option value="@ccc.val" @(ccc.val == selectedCountryId ? "selected" : "")>@ccc.text</option>
                    } 
                </select> 
            </td>
        </tr>
        <tr id="tabProvince" style="@(!selectThai ? "display: none;":"")">
            <td>จังหวัด</td>
            <td>
                <select id="province_id" name="province_id">
                    <option value="">จังหวัด</option>
    @foreach (var ccc in WSL.GetProvinces())
    {
        <option value="@ccc.Id" @(ccc.Id == selectedProvinceId ? "selected" : "")>@ccc.Name</option>
    }
</select> 
</td>
        </tr>
        <tr id="tabDistricts" style="@(!selectThai ? "display: none;":"")">
            <td>อำเภอ</td>
            <td>
                <select id="district_id" name="district_id">
                    <option value="">อำเภอ</option>
                    @foreach (var ccc in WSL.GetDistricts("" + selectedProvinceId))
                    {
                        <option value="@ccc.Id" @(ccc.Id == selectedDistrictId ? "selected" : "")>@ccc.Name</option>
                    }
                </select>
            </td>
        </tr>
        <tr id="tabSubDistricts" style="@(!selectThai ? "display: none;":"")">
            <td>ตำบล</td>
            <td>
                <select id="subdistrict_id" name="subdistrict_id">
                    <option value="">ตำบล</option>
                    @foreach (var ccc in WSL.GetSubDistricts("" + selectedDistrictId))
                    {
                        <option value="@ccc.Id" @(ccc.Id == selectedSubDistrictId ? "selected" : "")>@ccc.Name</option>
                    }
                </select>
            </td>
        </tr>
    </table>
        </div>

    <script type="text/javascript">

    var locationToRedirect = '';

    $(document).ready(function () {

        // if not found in url, check javascript, and redirect
        if ('@(string.IsNullOrWhiteSpace(Request["country_id"]+"")||(Request["country_id"]+"")=="undefined" ? "true":"")' && 
            parent && parent.formJson && parent.formJson.country_id) {
            var link = '/Complaint/GetCrime?a=crime&typereturn=alternate&country_id=' + parent.formJson.country_id +
                '&province_id=' + parent.formJson.province_id +
                '&district_id=' + parent.formJson.district_id +
                '&subdistrict_id=' + parent.formJson.subdistrict_id;
            $('body').empty();
            $('body').append('กำลังเปลี่ยนหน้าไป <meta http-equiv="refresh" content="0.1; url='+link+'" />' + link);
            // alert(link);
            // window.location = link;
        }

        $("#country_id").change(function () { 
            var Country = $("#country_id option:selected").text();

            if (Country == 'ไทย' || Country == 'Thailand') {
                $("#tabProvince , #tabDistricts , #tabSubDistricts").show();
                $('#district_id').html('<option value=""></option>');
                $('#subdistrict_id').html('<option value=""></option>');
            } else {
                $('#district_id').html('<option value=""></option>');
                $('#subdistrict_id').html('<option value=""></option>');
                $("#tabProvince , #tabDistricts , #tabSubDistricts").hide();
                selected_complete();
            }
        });
        $("#province_id").change(function () {
            $('#district_id').html('<option value="">กำลังโหลด...</option>');
            var url = "@Url.Content("~/Complaint/GetDistrictsJson")?provinceId=" + $(this).val();
            $.getJSON(url)
              .done(function (json) {
                  $('#district_id').html('<option value="">อำเภอ</option>');
                  $('#subdistrict_id').html('<option value="">ตำบล</option>');
                  if (!json || json.lenght == 0) {
                      alert('ไม่มีข้อมูลอำเภอภายใต้จังหวัดที่เลือก');
                  }
                  $.each(json, function (i, data) {
                      $('#district_id').append(
                          '<option value="' + data.Id + '">' + data.Name + '</option>'
                      );
                  }
              )
              })
              .fail(function (jqxhr, textStatus, error) {
                  var err = textStatus + ", " + error;
                  console.log("Request Failed: " + err);

              });
        });


        $("#district_id").change(function () {
            $('#subdistrict_id').html('<option value="">กำลังโหลด...</option>');
            var url = "@Url.Content("~/Complaint/GetSubDistrictsJson")?districtId=" + $(this).val();
            $.getJSON(url)
              .done(function (json) {
                  $.each(json, function (i, data) {
                      $('#subdistrict_id').html('<option value="">ตำบล</option>');
                      if (!json || json.lenght == 0) {
                          alert('ไม่มีข้อมูลตำบลภายใต้จังหวัดที่เลือก');
                      }
                      $('#subdistrict_id').append(
                          '<option value="' + data.Id + '">' + data.Name + '</option>'
                      );
                  }
              )
              })
              .fail(function (jqxhr, textStatus, error) {
                  var err = textStatus + ", " + error;
                  console.log("Request Failed: " + err);
              });
        });

        $("#subdistrict_id").change(function () {
            selected_complete();
        });
        // เมื่อได้ทำการเลือกข้อมูลจนครบ
        function selected_complete() {
             
            @if(Request["typereturn"]=="alternate")
            {
                <text>
            var country_id = $("#country_id").val();
            parent.formJson.country_id = country_id;

            var province_id = $("#province_id").val();
            parent.formJson.province_id = province_id;

            var district_id = $("#district_id").val();
            parent.formJson.district_id = district_id;

            var subdistrict_id = $("#subdistrict_id").val();
            parent.formJson.subdistrict_id = subdistrict_id;

            if ($("#country_id option:selected").text() == 'ไทย' || $("#country_id option:selected").text() == 'Thailand') {
                locationToRedirect += $("#subdistrict_id option:selected").text() + " ";
                locationToRedirect += $("#district_id option:selected").text() + " ";
                locationToRedirect += $("#province_id option:selected").text() + " ";
                locationToRedirect += $("#country_id option:selected").text();
            } else {
                locationToRedirect = $("#country_id option:selected").text();
            }

            var newHref = '@Url.Action("GetCrime", "Complaint")?a=crime&country_id=' + country_id +
                '&province_id=' + province_id + '&district_id=' + district_id + '&subdistrict_id=' + subdistrict_id;
             
            parent.formJson.locationToRedirect = locationToRedirect;
            parent.formJson.newHref = newHref;

            // console.log(locationToRedirect);
            </text>
            } else{
                <text>
            var country_id = $("#country_id").val();
            parent.$("#country_id").val(country_id);

            var province_id = $("#province_id").val();
            parent.$("#province_id").val(province_id);

            var district_id = $("#district_id").val();
            parent.$("#district_id").val(district_id);

            var subdistrict_id = $("#subdistrict_id").val();
            parent.$("#subdistrict_id").val(subdistrict_id);

            if ($("#country_id option:selected").text() == 'ไทย' || $("#country_id option:selected").text() == 'Thailand') {
                locationToRedirect += $("#subdistrict_id option:selected").text() + " ";
                locationToRedirect += $("#district_id option:selected").text() + " ";
                locationToRedirect += $("#province_id option:selected").text() + " ";
                locationToRedirect += $("#country_id option:selected").text();
            } else {
                locationToRedirect = $("#country_id option:selected").text();
            }

            var newHref = '@Url.Action("GetCrime", "Complaint")?a=crime&country_id=' + country_id +
                '&province_id=' + province_id + '&district_id=' + district_id + '&subdistrict_id=' + subdistrict_id;

            parent.addTextLocation2(locationToRedirect, newHref);

            console.log(locationToRedirect);
            </text>
            }
            parent.$.fn.colorbox.close();
        }
    });


</script>